package com.kongjs.application.controller.page;

import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.kongjs.application.model.entity.Posts;
import com.kongjs.application.service.IPostsService;
import com.kongjs.application.service.ITermRelationshipsService;
import com.kongjs.application.service.ITermsService;
import com.kongjs.common.rest.annotation.Page;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Page
@Controller
@RequestMapping("/search")
public class SearchPageController {

    @Resource
    private ITermsService termsService;
    @Resource
    private IPostsService postsService;
    @Resource
    private ITermRelationshipsService termRelationshipsService;

    @GetMapping
    public String index(Model model, @RequestParam(required = false) String q, @RequestParam(required = false, defaultValue = "1") Long pageNum, @RequestParam(required = false, defaultValue = "10") Long pageSize) {
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<Posts> page = postsService.lambdaQuery()
                .like(StringUtils.hasText(q), Posts::getPostTitle, q)
                .like(StringUtils.hasText(q), Posts::getPostContent, q)
                //.like(StringUtils.hasText(q), Posts::getPostAuthor, q)
                .page(PageDTO.of(pageNum, pageSize));
        model.addAttribute("postPage");
        return "search/index";
    }
}
